describe('App.MoveToPayeeListController', function() {
    var controller;
    beforeEach(function() {
        App.MoveToPayeeListController.reopen({
           init: function() {

           }
        });
        controller = App.MoveToPayeeListController.create({
            controllers: Ember.Object.create({
                moveHolder: Ember.Object.create({
                    prepareController: function() {

                    }
                })
            })
        });
    });

    it('prepareController', function() {
        var transferObject = {};

        spyOn(App.api_transfer, 'fetchToListForTransfer').andCallFake(function(transfer, callback){
            callback([], null);
        });

        spyOn(controller, 'toListCallback');
        spyOn(controller, 'showLoading');
        spyOn(App.getSession(), 'getCurrentTransfer').andReturn(transferObject);

        controller.prepareController();

        expect(controller.showLoading).toHaveBeenCalled();
        expect(App.api_transfer.fetchToListForTransfer).toHaveBeenCalledWith(transferObject, jasmine.any(Function));
        expect(controller.toListCallback).toHaveBeenCalled();
        expect(controller.get('transfer')).toEqual(transferObject);
    });

    describe('toListCallback', function() {
        it('there is error', function() {
            spyOn(controller, 'hasError').andReturn(true);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'set');

            controller.toListCallback([], null);

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.set).not.toHaveBeenCalled();
        });
        it('success response', function() {
            var list = [{},{}];
            spyOn(controller, 'hasError').andReturn(false);
            spyOn(controller, 'hideLoading');
            spyOn(controller, 'buildBillerListByType').andReturn(list);

            controller.toListCallback(list, null);

            expect(controller.hideLoading).toHaveBeenCalled();
            expect(controller.get('payeeList')).toEqual(list);
        });
    });
    it('selectPayeeType', function() {
        var payee = Ember.Object.create({
            toggleProperty: function() {
            }
        });

        spyOn(payee, 'toggleProperty');

        controller.selectPayeeType(payee);
        expect(payee.toggleProperty).toHaveBeenCalledWith('isOpened');
    })
    describe('buildBillerListByType', function() {
        it('empty list', function() {
            expect(controller.buildBillerListByType([]).length).toBe(0);
        });

        it('list with data', function() {
            var list = [
                {
                    billerInfo: App.Biller.create({
                        billerType: 'AIRLINES'
                    })
                },
                {
                    billerInfo: App.Biller.create({
                        billerType: 'INTERNET'
                    })
                },
                {
                    billerInfo: App.Biller.create({
                        billerType: 'AIRLINES'
                    })
                }
            ];
            var expectedList = [
                {
                    "isOpened": false,
                    "billerType": "AIRLINES",
                    "billers": [
                        {
                            "billerType": "AIRLINES",
                            "billerValidations":[]
                        },
                        {
                            "billerType": "AIRLINES",
                            "billerValidations":[]
                        }
                    ]
                },
                {
                    "isOpened": false,
                    "billerType": "INTERNET",
                    "billers": [
                        {
                            "billerType": "INTERNET",
                            "billerValidations":[]
                        }
                    ]
                }
            ];
            expect(JSON.stringify(controller.buildBillerListByType(list))).toEqual(JSON.stringify(expectedList));
        });
    });

    it('selectBiller', function() {
        var transferObject = Ember.Object.create({
            biller: null
        });
        var biller = Ember.Object.create();
        spyOn(App.getSession(), 'getCurrentTransfer').andReturn(transferObject);
        spyOn(controller, 'transitionTo');

        controller.selectBiller(biller);
        expect(transferObject.get('biller')).toBe(biller);
        expect(controller.transitionTo).toHaveBeenCalledWith('move.toPayeeDetailList');
    });
});